//
//  BiDismountContentViewController.m
//  TrunkIndustryManageSystem
//
//  Created by WT_lyy on 15/4/29.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiDismountContentViewController.h"

#import "DismountPartsCell.h"
#import "DismountHeadView.h"
#import "DismountFootView.h"
#import "DismountOverFootView.h"
#import "DismountOverHeadView.h"

#import "BiDismountEntity.h"
#import "BiFittingInfoEntity.h"

@interface BiDismountContentViewController ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>{
    
    UITableView * _tableView;
    
    NSMutableArray * _partsArray;
    
    
}

@end

@implementation BiDismountContentViewController

- (id)init{

    self = [super init];
    
    if (self) {
        
        _partsArray = [NSMutableArray new];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBackBtn];
    
    [self setNavBarWithHidden:NO];
    
    NSArray * arr = self.dentity.fittingInfoArray;
    
    [_partsArray addObjectsFromArray:arr];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, [self viewTop], MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - [self viewTop] -40)];
    
    _tableView.delegate = self;
    
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableView.rowHeight = 110;

    if (1 == self.type) {
        
        [self setNavBarTitle:@"拆装审批"];
        
        [self _initTableHeadFootForDismount];
    }else if(2 == self.type){
        
        [self setNavBarTitle:@"拆装完结审批"];
        
        [self _initTableHeadFootForDismountOver];
    }
    
    [self _initBottomButton];
}

- (void)_initTableHeadFootForDismount{
    
    DismountHeadView * headView = [[[NSBundle mainBundle] loadNibNamed:@"DismountHeadView" owner:self options:nil] lastObject];

    _tableView.tableHeaderView = headView;
    
    [headView dataFill:self.dentity];
    
    DismountFootView * footView = [[[NSBundle mainBundle] loadNibNamed:@"DismountFootView" owner:self options:nil] lastObject];
    
    _tableView.tableFooterView = footView;
    footView.textViRsn.delegate = self;
    footView.labApprover.text = self.dentity.dismountApprovePersonName.length==0?[BiUserEntity sharedInstance].name:self.dentity.dismountApprovePersonName;
    footView.labApproveTime.text = self.dentity.dismountApproveDate.length==0?[BiUtil stringFromFomate:[NSDate date] formate:@"yyyy-MM-dd hh:ss"]:self.dentity.dismountApproveDate;
    footView.textViRsn.text = self.dentity.dismountRejectReason;
}

- (void)_initTableHeadFootForDismountOver{
    
    DismountOverHeadView * headView = [[[NSBundle mainBundle] loadNibNamed:@"DismountOverHeadView" owner:self options:nil] lastObject];
    
    _tableView.tableHeaderView = headView;
    [headView dataFill:self.dentity];
    
    
    DismountOverFootView * footView = [[[NSBundle mainBundle] loadNibNamed:@"DismountOverFootView" owner:self options:nil] lastObject];
    
    _tableView.tableFooterView = footView;
    footView.textViRsn.delegate = self;
    [footView dataFill:self.dentity];
}

- (void)_initBottomButton{

    UIButton * btnsave = GET_BUTTON(CGRectMake(0, MAIN_SCREEN_HEIGHT-40, MAIN_SCREEN_WIDTH/2, 40), 15, NO, [UIColor whiteColor]);
    
    [self.view addSubview:btnsave];
    btnsave.tag = 1;
    btnsave.backgroundColor = UIColorFromRGB(BLUE_COLOR_VALUE);
    [btnsave setTitle:@"批准" forState:UIControlStateNormal];
    [btnsave addTarget:self action:@selector(btnSaveCheckClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * btnCheck = GET_BUTTON(CGRectMake(MAIN_SCREEN_WIDTH/2+1, MAIN_SCREEN_HEIGHT-40, MAIN_SCREEN_WIDTH/2, 40), 15, NO, [UIColor whiteColor]);
    
    [self.view addSubview:btnCheck];
    btnCheck.tag = 2;
    btnCheck.backgroundColor = UIColorFromRGB(BLUE_COLOR_VALUE);
    [btnCheck setTitle:@"驳回" forState:UIControlStateNormal];
    [btnCheck addTarget:self action:@selector(btnSaveCheckClick:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -Button Action

- (void)btnSaveCheckClick:(UIButton *)butn{
    
    NSDictionary * paraDic = nil;
    
    if (1 == self.type) {
        
        NSString * appPerId = self.dentity.dismountApprovePersonId.length==0?[BiUserEntity sharedInstance].userid:self.dentity.dismountApprovePersonId;
        
        NSString * appDate = self.dentity.dismountApproveDate.length == 0?[BiUtil stringFromFomate:[NSDate date] formate:@"yyyy-MM-dd hh:ss"]:self.dentity.dismountApproveDate;
        
        if (1 == butn.tag) {
            
            paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.dentity.e_id,@"id",
                       @"2",@"dismount_status",
                       @"1",@"status",
                       appPerId ,@"dismountApprovePersonId",
                       appDate,@"dismountApproveDate",
                       self.dentity.dismountRejectReason,@"dismountRejectReason",nil];
        }else{
            paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.dentity.e_id,@"id",
                       @"2",@"dismount_status",
                       @"0",@"status",
                       appPerId ,@"dismountApprovePersonId",
                       appDate,@"dismountApproveDate",
                       self.dentity.dismountRejectReason,@"dismountRejectReason",nil];
            
        }
    }else{
        
        NSString * appDate = [BiUtil stringFromFomate:[NSDate date] formate:@"yyyy-MM-dd hh:ss"];
        
        if (1 == butn.tag) {
            
            paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.dentity.e_id,@"id",
                       @"5",@"dismount_status",
                       @"1",@"status",
                       [BiUserEntity sharedInstance].userid ,@"dismountApprovePersonId",
                       appDate,@"dismountApproveDate",
                       self.dentity.dismountRejectReason,@"dismountRejectReason",nil];
        }else{
            paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.dentity.e_id,@"id",
                       @"5",@"dismount_status",
                       @"0",@"status",
                       [BiUserEntity sharedInstance].userid ,@"recoverApprovePersonId",
                       appDate,@"recoverApproveDate",
                       self.dentity.dismountAcceptanceRejectReason,@"dismountAcceptanceRejectReason",nil];
            
        }

    }

    [[BiNetEngine shareInstance] requestDismountCarCheck:self paraDict:paraDic];
    
    [self asyshowHUDView:WAITING CurrentView:self];
}


#pragma mark -textview delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([@"\n" isEqualToString:text] == YES)
    {
        [textView resignFirstResponder];
        
        
        return NO;
    }
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    
    _tableView.frame = CGRectMake(0, _tableView.frame.origin.y - 190, MAIN_SCREEN_WIDTH, _tableView.frame.size.height);
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    
    _tableView.frame = CGRectMake(0, _tableView.frame.origin.y + 190, MAIN_SCREEN_WIDTH, _tableView.frame.size.height);
}

#pragma -mark UitabeViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma -mark UitabeViewDatasource

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_partsArray) {
        
        return _partsArray.count;
    }
    
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * myIdentify = @"DismountPartsCell";
    
    DismountPartsCell * cell = (DismountPartsCell *)[tableView dequeueReusableCellWithIdentifier:myIdentify];
    
    if (!cell) {
        
        cell =  [[[NSBundle mainBundle] loadNibNamed:@"DismountPartsCell" owner:self options:nil] lastObject];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    BiFittingInfoEntity * finfo = _partsArray[indexPath.row];
    
    [cell fittingDataFill:finfo];
    
    return cell;
}


#pragma -mark Net Delegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    NSDictionary * entity_ = (NSDictionary *)entity;
    
    //[self showMessage:entity_[@"result_info"]];
    
    if ([entity_[@"response_status"] integerValue] != 200) {
        
        return;
        
    }
    
    
    switch (tag) {
        case NET_GET_DISMOUNT_MOUNT_CHECK:

         //   sleep(2);
            
            [self.navigationController popViewControllerAnimated:YES];
            
            break;
        default:
            break;
    }
    
}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    [self showMessage:@"请求超时！"];
}

@end
